package com.swm.julie.rest.sausage.index.korean;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.TermVector;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Assert;

import com.swm.julie.rest.sausage.index.field.Product;


public class SausageIndexer {

	String indexDir;
	private IndexWriter writer;

	public SausageIndexer(String indexDir) throws IOException
	{
		this.indexDir = indexDir;
		Directory directory = FSDirectory.open(new File(indexDir));

		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, new CJKAnalyzer(Version.LUCENE_34));
		writer = new IndexWriter(directory, config);
	}

	/**
	 * write the document with product model
	 * 
	 * @param product
	 * @throws CorruptIndexException
	 * @throws IOException
	 */
	public void indexingProduct(Product product) throws CorruptIndexException, IOException
	{
		System.out.println("SausageIndexer.indexingProduct()");
		
		//index directory null test
		Assert.assertNotNull(indexDir);
		
		Document doc = getDocument(product);

		// null test of the product argument and returned document
		Assert.assertNotNull("product argument null test", product);
		Assert.assertNotNull(doc);

		writer.addDocument(doc);
	}

	/**
	 * Return the document of the product model with each field.
	 * 
	 * @param product
	 * @return
	 */
	private Document getDocument(Product product) 
	{
		System.out.println("SausageIndexer.getDocument()");

		Document doc = new Document();
		
		doc.add(new Field("company", false, product.getCompany(), Field.Store.YES, Index.ANALYZED, TermVector.NO));
		doc.add(new Field("itemName", false, product.getItemName(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("image", false, product.getImage(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("url", false, product.getUrl(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("item", false, product.getItem(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("locate", false, product.getLocate(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("discountRatio", false, String.valueOf(product.getDiscountRatio()), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("price", false, product.getPrice(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("shopName", false, product.getShopName(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("shopPhone", false, product.getShopPhone(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("shpoLocate", false, product.getPrice(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("buyStart", false, product.getBuyStart(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("buyEnd", false, product.getBuyEnd(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("useStart", false, product.getUseStart(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		doc.add(new Field("useEnd", false, product.getUseEnd(), Field.Store.YES, Index.ANALYZED, TermVector.YES));
		
		return doc;
	}

	/**
	 * close index writer
	 * @throws CorruptIndexException
	 * @throws IOException
	 */
	public void close() throws CorruptIndexException, IOException
	{
		writer.close();
	}
	
	public String getIndexDir() {
		return indexDir;
	}

	public void setIndexDir(String indexDir) {
		this.indexDir = indexDir;
	}

}
